<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\Recommend;
use App\Models\Product;
use App\Http\Controllers\Controller;
use App\Models\Shop;
use App\Models\User;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class ProductsController extends Controller
{
    use HasResourceActions;

    private $shop_id;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.products'))
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.products'))
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.products'))
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header(trans('admin.menu_titles.products'))
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Product);
        $grid->model()->orderBy('sort', 'asc');
        $grid->id('Id');
        $grid->shop(trans('admin.shop'))->display(function ($value) {
            return $value['name'];
        });
        $grid->title(trans('admin.title'))->limit(20)->editable();
        $grid->subtitle(trans('admin.subtitle'))->limit(20)->editable('textarea');
        $grid->image(trans('admin.face_cover'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->content(trans('admin.content'))->limit(20);
        $grid->pictures(trans('admin.pictures'))->lightbox(['width' => 50, 'height' => 50]);
        $grid->contact_phone(trans('admin.contact_phone'));
        $grid->service_time(trans('admin.service_time'))->expand(function ($model) {
            $schedules = $model->schedules()->get()->map(function ($schedule) {
                return $schedule->only(['id', 'price', 'begin_time', 'end_time', 'status_format', 'created_at']);
            });

            return new Table(['ID', trans('admin.price'), trans('admin.begin_time'), trans('admin.end_time'), trans('admin.status'), trans('admin.created_at')], $schedules->toArray());
        });
//        $grid->stock_amount(trans('admin.stock'));
        $grid->view_amount(trans('admin.view_amount'))->badge('light');
        $grid->sell_amount(trans('admin.sell_amount'))->badge('red');
        $grid->is_show(trans('admin.show'))->switch();
        $grid->sort(trans('admin.sort'))->orderable();
        $grid->created_at(trans('admin.created_at'));
//        $grid->updated_at(trans('admin.updated_at'));
        $grid->actions(function ($action) {
            $action->prepend(new Recommend($action->getKey(), $action->row->recommended, 'Product'));
        });
        $grid->filter(function ($filter) {
            $filter->column(1 / 3, function ($filter) {

            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('title', trans('admin.title'));
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->between('price', trans('admin.price'));
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Product::findOrFail($id));

        $show->id('Id');
        $show->shop(trans('admin.shop'))->as(function ($value) {
            return $value['name'];
        });
        $show->title(trans('admin.title'));
        $show->subtitle(trans('admin.subtitle'));
        $show->image(trans('admin.image'))->image();
        $show->contact_phone(trans('admin.contact_phone'));
        $show->content(trans('admin.content'));
        $show->pictures(trans('admin.pictures'))->image();
        $show->service_time(trans('admin.service_time'));
        $show->price(trans('admin.price'));
//        $show->stock_amount(trans('admin.stock'));
        $show->view_amount(trans('admin.view_amount'));
        $show->sell_amount(trans('admin.sell_amount'));
        $show->sort(trans('admin.sort'));
        $show->created_at(trans('admin.created_at'));
        $show->updated_at(trans('admin.updated_at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $shop_id = Request()->get('shop_id');

        $form = new Form(new Product);

        $form->select('shop_id', trans('admin.shop'))
            ->options(Shop::where('status', Shop::PASS)->pluck('name', 'id'))
            ->default($shop_id)->rules('required')->setLabelClass(['required']);
        $form->text('title', trans('admin.title'))->rules('required')->setLabelClass(['required']);
        $form->text('subtitle', trans('admin.subtitle'));
        $form->mobile('contact_phone', trans('admin.contact_phone'));
        $form->number('service_time', trans('admin.service_time'))->min(0);
        $form->image('image', trans('admin.image'));
        $form->textarea('content', trans('admin.content'));
        $form->multipleImage('pictures', trans('admin.pictures'));
//        $form->number('stock_amount', trans('admin.stock'))->default(0)->min(0);
        $form->number('view_amount', trans('admin.view_amount'))->default(0)->min(0);
        $form->number('sell_amount', trans('admin.sell_amount'))->default(0)->min(0);
//        $form->number('sort', trans('admin.sort'))->default(0)->min(0);
        $form->switch('is_show', trans('admin.show'));
//        $form->disableEditingCheck();
        $form->disableCreatingCheck();
//        $form->disableViewCheck();
        $form->tools(function (Form\Tools $tools) use ($shop_id) {
            $tools->disableDelete();
            $tools->disableView();

            if ($shop_id) {
                $tools->disableList();
                $tools->append('<div class="btn-group pull-right" style="margin-right: 5px">
<a href="' . route('shops.show', ['shop' => $shop_id]) . '" class="btn btn-sm btn-primary" title="' . trans('admin.back') . '"><i class="fa fa-backward"></i><span class="hidden-xs">&nbsp;' . trans('admin.back') . '</span></a></div>');
            } else {
                $tools->append('<div class="btn-group pull-right" style="margin-right: 5px">
<a href="javascipt:;" onClick="javascript :history.back();" class="btn btn-sm btn-primary" title="' . trans('admin.back') . '"><i class="fa fa-backward"></i><span class="hidden-xs">&nbsp;' . trans('admin.back') . '</span></a></div>');
            }
        });
        $form->hasMany('schedules', trans('admin.schedules'), function ($form) {
            $form->decimal('price', trans('admin.price'))->default(0.00)->rules(['required', 'numeric', 'min:0'])->setLabelClass(['required']);
            $form->datetimeRange('begin_time', 'end_time', trans('admin.schedules'))->rules('required')->setLabelClass(['required']);
        });
        $form->saved(function ($form) use ($shop_id) {
//            if ($shop_id) {
//                return redirect(route('shops.show', ['shop' => $shop_id]));
//            }
        });

        return $form;
    }
}
